package com.huidz.calendar.activityadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huidz.calendar.R;
import com.huidz.calendar.model.BWLModel;
import com.huidz.calendar.util.LunarCalendar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class JiNianRiAdapter extends BaseAdapter {
    ArrayList<BWLModel.data> list;
    Context context;

    public JiNianRiAdapter(ArrayList<BWLModel.data> list, Context context) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    int snum=0;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.srblist, null);
        init i = new init();
        i.list_time_rela = convertView.findViewById(R.id.list_time_rela);
        i.all = convertView.findViewById(R.id.all);
        i.bwl_list_msg = convertView.findViewById(R.id.bwl_list_msg);
        i.bwl_list_msg_time = convertView.findViewById(R.id.bwl_list_msg_time);
        i.bwl_list_time = convertView.findViewById(R.id.bwl_list_time);
        i.bwl_list_time_text = convertView.findViewById(R.id.bwl_list_time_text);
        i.srb_list_msg = convertView.findViewById(R.id.srb_list_msg);
        i.srb_list_msg_time = convertView.findViewById(R.id.srb_list_msg_time);
        i.bwl_list_day = convertView.findViewById(R.id.bwl_list_day);
        i.bwl_list_hour = convertView.findViewById(R.id.bwl_list_hour);

        if (!list.get(position).getType().equals("3")){
            i.all.setVisibility(View.GONE);
        }
        else {
            snum++;
            if (snum == 1) {
                i.list_time_rela.setVisibility(View.VISIBLE);
                long sjc = Long.valueOf(list.get(position).getRemindtime());
                String times = getDateToString(sjc);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //创建对象制定日期格式
                String dates = format.format(new Date());
                i.bwl_list_day.setText(dateDiff(dates,times)+"");
                i.bwl_list_hour.setText(hour+"");
                i.bwl_list_time.setText("距离"+list.get(position).getCommem()+"的纪念日还有");
            }
            i.bwl_list_msg.setText(list.get(position).getCommem());
            long sjc = Long.valueOf(list.get(position).getRemindtime());
            String time = getDateToString(sjc);
            i.srb_list_msg.setText(r+"");
            i.srb_list_msg_time.setText(m+"月");
            i.bwl_list_msg_time.setText(time + " " + s + " " + getWeek(sjc));
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //创建对象制定日期格式
            String date = format.format(new Date());
            if(dateDiff(date,time)==0){
                i.bwl_list_time_text.setText("已过期");
                i.bwl_list_time_text.setTextColor(context.getResources().getColor(R.color.title_color));
            }
            else
            i.bwl_list_time_text.setText(dateDiff(date,time)+"天");
        }
        convertView.setTag(i);
        return convertView;
    }

    public static class init {
        //首页list的子控件
        TextView bwl_list_msg, bwl_list_msg_time, bwl_list_time, bwl_list_time_text, srb_list_msg, srb_list_msg_time, bwl_list_day, bwl_list_hour;
        RelativeLayout list_time_rela,all;
    }

    public static int  y,m,r;
    public static String s;
    public static String getDateToString(long milSecond) {
        Date date = new Date(milSecond * 1000);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        y = Integer.parseInt(format.format(date).substring(0, 4));
        m = Integer.parseInt(format.format(date).substring(5, 7));
        r = Integer.parseInt(format.format(date).substring(8, 10));
        s = LunarCalendar.getLunarString(y, m, r);
        return format.format(date);
    }

    public static String getWeek(long time) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(time * 1000));
        int year = cd.get(Calendar.YEAR); //获取年份
        int month = cd.get(Calendar.MONTH); //获取月份
        int day = cd.get(Calendar.DAY_OF_MONTH); //获取日期
        int week = cd.get(Calendar.DAY_OF_WEEK); //获取星期
        String weekString;
        switch (week) {
            case Calendar.SUNDAY:
                weekString = "周日";
                break;
            case Calendar.MONDAY:
                weekString = "周一";
                break;
            case Calendar.TUESDAY:
                weekString = "周二";
                break;
            case Calendar.WEDNESDAY:
                weekString = "周三";
                break;
            case Calendar.THURSDAY:
                weekString = "周四";
                break;
            case Calendar.FRIDAY:
                weekString = "周五";
                break;
            default:
                weekString = "周六";
                break;

        }
        return weekString;
    }

    long hour=0;

    public long dateDiff(String startTime, String endTime) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            if (day>=1) {
                return day;
            }else {
                if (day==0) {
                    return 1;
                }else {
                    return 0;
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }

}

